#ifndef FTPMODEL_H
#define FTPMODEL_H

#include <QAbstractListModel>
#include <QFtp>
#include <QList>
#include <QUrlInfo>
#include <QFile>


class FtpModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(bool loading READ loading WRITE setLoading NOTIFY loadingChanged)
    Q_PROPERTY(QString currentPath READ currentPath WRITE setCurrentPath NOTIFY currentPathChanged)
    Q_PROPERTY(float progress READ progress WRITE setProgress NOTIFY progressChanged)
    Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
    Q_PROPERTY(int state READ state NOTIFY stateChanged)
    Q_PROPERTY(QString connectionName READ connectionName NOTIFY connectionNameChanged)
    Q_PROPERTY(int taskCount READ taskCount NOTIFY taskCountChanged)




public:
    enum FileRoles {
        nameRole = Qt::UserRole + 1,
        isDirRole,
        isFileRole,
        sizeRole,
        lastModifiedRole
    };

    explicit FtpModel(QObject *parent = 0);
    ~FtpModel();
    int rowCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    void setLoading(bool l){mLoading = l;}
    bool loading(){return mLoading;}
    void setCurrentPath(const QString& path){mCurrentPath = path;}
    const QString& currentPath(){return mCurrentPath;}
    void setProgress(float p){mProgress = p;}
    float progress(){return mProgress;}
    int state(){return mFtp->state();}
    const QString& message(){return mMessage;}
    void setMessage(const QString& m ){mMessage = m;}
    const QString& connectionName(){return mConnectionName;}
    int taskCount();

  Q_INVOKABLE QString stateName(int state);



    //command




signals:
    void loadingChanged();
    void currentPathChanged();
    void progressChanged();
    void stateChanged();
    void messageChanged();
    void connectionNameChanged();
    void taskCountChanged();

public slots:
    void connectToHost(const QString& host, quint16 port = 21);
    void login(const QString& user = QString(),const QString& password = QString());
    void refresh(const QString& dir = QString());
    void showState(int state);
    void done(bool error);
    void addFile(const QUrlInfo &i);
    void close();
    void openFolder(int index);
    void back();
    void removeFile(int index);
    void mkdir(const QString& dir);
    void downloadFile(int index, const QString& dest = QString());
    void renameFile(int index, const QString& name);
    void uploadFile(const QString& path);
     QString destDir(){return mDestDir;}




protected slots:
    void dataTransferProgress(qint64 byte, qint64 total);
    void commandFinished(int id, bool error);
    void rawCommandReply(int replyCode, const QString& detail);
    void clear();


private:
    QFtp * mFtp;
    QList <QUrlInfo> mFilesList;
    bool mLoading;
    QString mCurrentPath;
    QString mMessage;
    QString mDestDir;
   QString mConnectionName;
    float mProgress;

    QList <QFile*> mDownloadFiles;
    QList <QFile*> mUploadFiles;

    int mDownloadId;
    int mUploadId;

};

#endif // FTPMODEL_H
